﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
  public   class stockPositionBLL
    {
        private stockPositionTableAdapter _stockPositionAdapter = null;
        private stockPositionTableAdapter stockPositionAdapter
        {
            get
            {
                if (_stockPositionAdapter == null) _stockPositionAdapter = new stockPositionTableAdapter();
                return _stockPositionAdapter;
            }
        }
        /// <summary>
        /// 查询仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.stockPositionDataTable getstockPositionAll()
        {
            try
            {
                return stockPositionAdapter.GetDatastockPositionAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据仓位名称查询仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public orderDataSet.stockPositionDataTable getstockPositionAll(string posName)
        {
            try
            {
                return stockPositionAdapter.GetDatastockPositionByposName(posName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 插入仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int stockPositionInsert(stockPositionModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockPositionDAL stockPosition = new stockPositionDAL(tn, conn);
                stockPosition.stockPositionInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个仓位名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 插入仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int stockPositionTableInsert(orderDataSet.stockPositionDataTable  stockPositionTable)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockPositionDAL stockPosition = new stockPositionDAL(tn, conn);
                stockPosition.stockPositionTableInsert(stockPositionTable);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个仓位名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int stockPositionUpdate(stockPositionModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockPositionDAL stockPosition = new stockPositionDAL(tn, conn);
                stockPosition.stockPositionUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行仓位信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int stockPositionDel(string posName)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                stockPositionDAL stockPosition = new stockPositionDAL(tn, conn);
                stockPosition.stockPositionDel(posName);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
